﻿using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using CommonModel.Model;
using CommonModel.Util;
using static CommonModel.Model.DataTypeDll;

namespace PtlImp
{
    class Class1
    {
        public static int ByteToUint16(byte[] data)
        {
            return (data[1] << 8 & 0xFF00 | data[0] & 0xFF);
        }

        //字节数组转16位uint，数据类型是16位、有符号的以二进制补码表示的整数，占2字节，大端
        public static int ByteToUint16BigEndian(byte[] data)
        {
            Array.Reverse(data);
            return (data[0] << 8 & 0xFF00 | data[1] & 0xFF);
        }
        static void Main(string[] args)
        {
            //检查本机支持的编码
            var encodeArr = System.Text.Encoding.GetEncodings();
            Console.WriteLine(encodeArr);
            string str = "544300005543000054430000e841000082420000e04100323032302d342d382031363a30363a333020202020bfd8d6c6c3fcc1eeb2d9d7f7b3c9b9a60d323032302d342d382031363a30363a323820202020b2d9d7f7c1f7b3ccbde1caf80d323032302d342d382031363a30363a323720202020b7a2cbcd20202020d7aabbbbbfd8d6c6c6f7ceaa20202020d7d4b6af20202020d6b8c1ee0d323032302d342d382031363a30363a323320202020bfd8d6c6c3fcc1eeb2d9d7f7b3c9b9a60d323032302d342d382031363a30363a323120202020b7a2cbcd20202020cda3d6b9b7a2b5e7bbfa20202020d6b8c1ee0d323031362d372d3120393";
            byte[] data = TransForm.HexToByte(str, false);
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            string newstr = Encoding.GetEncoding(TransForm.encode).GetString(data, 0, data.Length).ToString();
            Console.WriteLine(newstr);
            /*byte[] data = {109,149};
            int a= ByteToUint16(data);
            Console.WriteLine(a);*/

            /*            byte[] datax = new byte[1];
                        datax[0] = bitArray[0];
                        int i = 0;
                        if (i < bitArray.Length)
                        {
                            Console.WriteLine(bitArray[i]);
                            i++;
                        }
                        string value = "";
                        value = BytesParseToTypeString.ParseValue(DataType.BYTE, datax);*/
            //Console.WriteLine(value);
            //string path = Environment.CurrentDirectory + @"/xml/";
            //string data_30W = "C0A80381D204C8CF7E467901000000255A30313031333330393030000000000000FC0302071E33030035A3540020000000009EB7893C225A114242D1CF41008088C3206D95C19CCBBF41000000004ED4B03C000000000000F0410004FFFF180C";
            //byte[] data = TransForm.HexToByte(data_30W, false);
            //Console.WriteLine(data);
            ////30W模拟调频发射机。通用自研：第一字节25，第二字节5A，十六进制
            //byte[] flag_1 = data.Skip(15).Take(1).ToArray();
            //byte[] flag_2 = data.Skip(16).Take(1).ToArray();
            //string flag1 = TransForm.ByteToHexString(flag_1,  true);//转为十六进制字符串
            //string flag2 = TransForm.ByteToHexString(flag_2, true);
            //HeaderData header = null;
            //if (flag1.Equals("25") && flag2.Equals("5A"))
            //{
            //    header = ParseSvr.ByteParseToHead30Wor50W(data);
            //}
            //else
            //{
            //    header = ParseSvr.ByteParseToHeadDigit50W(data);
            //}
            //string schemaName = header.Device + "_" + header.Vendor + "_" + header.Hardware + "_" + header.Software + "_" + header.MsgType;
            //Console.WriteLine(schemaName);
            //CfgLoader.SchemaServer.initSchemaMap(path);
            //Console.WriteLine(CfgLoader.SchemaServer.SCHAMA_DICT);//20210402已经把XML配置文件都顺利解析成规则字典，可以根据xmlType来获取对应的解析规则XML文件了
            //SchemaDocument doc = CfgLoader.SchemaServer.SCHAMA_DICT[schemaName];// 获取对应的xml解析文件,字段名存放在fieldDictionary中
            //if (header.MsgType == "300/1000")
            //{
            //    ParseSvr.ByteParseViaCode(doc, data, "300/1000");
            //}
            //else
            //if (header.MsgType == "code")
            //{
            //    ParseSvr.ByteParseViaCode(doc, data, "");
            //}
            //else
            //{
            //    ParseSvr.ByteParseViaStruct(doc, data);
            //}

            /*string[] xmls = SchemaServer.getDirFiles("D:/vs2019_project/ptlsvrprj/xml");//获取所有的xml配置文件
            Console.WriteLine(xmls);*/
            //SchemaServer.initSchemaMap("D:/vs2019_project/ptlsvrprj/xml");
            //XElement xe = XElement.Load("D:/vs2019_project/ptlsvrprj/xml/0x0201_30W_AutoSend.xml");
            /*System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            XmlDocument doc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create("D:/vs2019_project/ptlsvrprj/xml/0x0201_30W_AutoSend.xml", settings);
            doc.Load(reader);
            XmlNode xn = doc.SelectSingleNode("schema");
            XmlElement xe = (XmlElement)xn;
            string name = xe.GetAttribute("name").ToString();
            Console.WriteLine(xe.ChildNodes.Count);
            string msgType = xe.GetAttribute("msgType").ToString();
            Console.WriteLine(name);
            XmlNodeList xnl = xn.ChildNodes;

            DataType fieldType = DataTypeUtils.getType("bit");
            Console.WriteLine(DataTypeUtils.getType("bit"));*/

            /*            Regex regex = new Regex("\\$\\{(.//)\\}");
                        string loop = "${FieldCount}";
                        if (regex.IsMatch(loop))
                        {
                            //string relateFieldName = match.group(1);
                            loop = loop.Replace("${", "");
                            loop = loop.Replace("}", "");
                            Console.WriteLine(loop);

                        }*/


            //根据type来选择code还是struce进行帧体的解析
            /*if (header.memo.equalsIgnoreCase("300/1000"))
            {
                // dataSegmentParser = new ByteParseViaCode_300_1000(logger);// 300W/1000w特有解析方法
                dataSegmentParser = new ByteParseViaCode(logger, DeviceModelType.M_300_1000); // 300/1000W地面数字发射机命令码解析
            }*/
            /*            if (type.Equals("code"))
                        {
                            dataSegmentParser = new ByteParseViaCode(DeviceModelType.M_000); // 非 300/1000W地面数字发射机命令码解析
                        }
                        else
                        {
                            dataSegmentParser = new ByteParseViaStruct();
                        }

                        segment = dataSegmentParser.parse(doc, dataBody);*/


            /*   //byte[] datax = Arrays.copyOfRange(data, currentCursor, currentCursor + size);

         int currentCursor = 0;
            int size = 4;
            int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            int[] datax = new int[currentCursor + size];
            Array.Copy(data, currentCursor, datax, 0, currentCursor + size);
            Console.WriteLine(datax);*/
        }
    }
}
